package com.zlx.guguanjia.aspect;

import com.zlx.guguanjia.entity.SysLog;
import com.zlx.guguanjia.entity.SysUser;
import com.zlx.guguanjia.service.SysLogService;
import com.zlx.guguanjia.util.IPUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author 周罗校
 * @Description 日记的切面类
 * @ClassName LogAspect
 * @date 2020.11.09 13:37:37
 * 该版权归周罗校所有，最终解释权以官方为准
 */
@Aspect
@Component
public class LogAspect {
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private HttpServletRequest request;

    @Pointcut("within(com.zlx.guguanjia.service.impl.*Impl)")
    public void pointcut(){

    }

    /**
     * @Author 周罗校
     * @Description 正常的日志
     * @Param []
     * @return java.lang.Object
     * @Date 2020.11.09 13:49:16
     **/
    @AfterReturning(pointcut = "pointcut()",returning = "obj")
    public Object after(JoinPoint joinpoint, Object obj){
        String className = joinpoint.getTarget().getClass().getSimpleName();
        //排除SysLogServiceImpl，防止递归的死循环
        if(!className.equals("SysLogServiceImpl")) {
            insert(null, joinpoint);
        }
        return obj;
    }
    @AfterThrowing(pointcut = "pointcut()",throwing = "e")
    public void AfterThrowing(JoinPoint joinpoint, Exception e){
        insert(e,joinpoint);
    }

    public void insert(Exception e,JoinPoint joinPoint){
        SysLog sysLog = new SysLog();
        //日志类型，1：异常，2：正常
        sysLog.setType(e==null?"1":"2");
        if (request!=null){
            SysUser loginUser = (SysUser) request.getSession().getAttribute("loginUser");
            if (loginUser!=null){
                sysLog.setCreateBy(loginUser.getName());
            }
            sysLog.setCreateDate(new Date());
            sysLog.setRemoteAddr(IPUtils.getClientAddress(request));
            //客户端信息
            sysLog.setRequestUri(request.getRequestURI());
            sysLog.setUserAgent(request.getHeader("User-Agent"));
            sysLog.setMethod(request.getMethod());
        }
        Object[] args = joinPoint.getArgs();
        if (args!=null&&args.length>0){
            //[参数1，类型:SysUser，值:{"password":"aaaa","mobile":"139999"}]
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i <args.length ; i++) {
                if (args[i]!=null) {
                    sb.append("[参数" + i).
                            append(",类型" + args[i].getClass().getSimpleName()).
                            append(",值：" + args[i]).
                            append("],");
                }
            }
            sb.deleteCharAt(sb.length()-1);
            sysLog.setParams(sb.toString());
        }
        sysLog.setException(e==null?"":e.toString());
        sysLog.setDescription("记录日志。。。。");

        sysLogService.insertSelective(sysLog);
    }
}
